package com.itheima.wemedia.controller;


import com.itheima.common.dto.PageRequestDto;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmMaterialPageReqDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.service.WmMaterialService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import com.itheima.core.controller.AbstractCoreController;

import java.util.List;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.core.toolkit.Wrappers.query;

/**
 * @description <p>自媒体图文素材信息</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.controller
 */
@Api(value="WmMaterialController",tags = "自媒体图文素材信息")
@RestController
@RequestMapping("/material")
public class WmMaterialController extends AbstractCoreController<WmMaterial> {

    private WmMaterialService wmMaterialService;

    @Autowired
    public WmMaterialController(WmMaterialService wmMaterialService) {
        super(wmMaterialService);
        this.wmMaterialService=wmMaterialService;
    }

    /**
     *分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public PageResultVo<WmMaterial> findPage(@RequestBody WmMaterialPageReqDto dto){
        PageResultVo<WmMaterial> list = wmMaterialService.findPage(dto);
        List<WmMaterial> data = (List<WmMaterial>) list.getData();
        data.forEach(m->m.setUrl("http://192.168.211.128:8080/"+m.getUrl()));
        return list;
    }

    /**
     * 收藏
     * @return
     */
    @GetMapping("/collect/{id}")
    public ResultVo<WmMaterial> Collect(@PathVariable(name = "id")Integer id){
        //根据id去更新IsCollection字段 1为收藏0为否
        int row = wmMaterialService.collect(id);
        if (row>0){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }
    /**
     * 取消收藏
     * @return
     */
    @GetMapping("/cancel_collect/{id}")
    public ResultVo<WmMaterial> cancelCollect(@PathVariable(name = "id")Integer id){
        //根据id去更新IsCollection字段 1为收藏0为否
        int row = wmMaterialService.cancelCollect(id);
        if (row>0){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @GetMapping("/del_picture/{id}")
    public ResultVo<WmMaterial> delete(@PathVariable(name = "id")Integer id){
        int row = wmMaterialService.delete(id);
        if (row>0){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }



}

